package com.alander.nexusgo.user.controller;

import com.alander.nexusgo.common.model.dto.CheckinDTO;
import com.alander.nexusgo.common.model.vo.CheckinVO;
import com.alander.nexusgo.common.response.Result;
import com.alander.nexusgo.framework.web.starter.controller.BaseController;
import com.alander.nexusgo.user.service.UserCheckinsService;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.alander.nexusgo.framework.mysql.starter.page.PageUtils.getPageInfo;
import static com.alander.nexusgo.framework.mysql.starter.page.PageUtils.startPage;

/**
 * 用户打卡管理
 */
@RestController
@RequestMapping("/api/user/checkins")
@RequiredArgsConstructor
public class UserCheckinsController extends BaseController {

    private final UserCheckinsService userCheckinsService;

    /**
     * 用户打卡
     *
     * @param checkinDTO 打卡信息
     * @return 打卡结果
     */
    @PostMapping("/checkin")
    public Result<CheckinVO> checkin(CheckinDTO checkinDTO) {
        try {
            // 设置上传的文件
            checkinDTO.setPhotos(checkinDTO.getPhotos());
            CheckinVO checkinVO = userCheckinsService.checkin(checkinDTO);
            return success(checkinVO);
        } catch (Exception e) {
            return fail(500, e.getMessage());
        }
    }


    /**
     * 获取用户打卡记录（分页）
     *
     * @param userId 用户ID
     * @param pageNum 页码
     * @param pageSize 每页大小
     * @return 打卡记录列表
     */
    @GetMapping("/list/{userId}")
    public PageInfo<CheckinVO> listUserCheckins(@PathVariable(value = "userId") Long userId,
                                                @RequestParam(defaultValue = "1") Integer pageNum,
                                                @RequestParam(defaultValue = "10") Integer pageSize) {
        startPage(pageNum, pageSize);
        List<CheckinVO> checkinList = userCheckinsService.listUserCheckins(userId);
        return getPageInfo(checkinList);
    }

    /**
     * 获取打卡详情
     *
     * @param checkinId 打卡记录ID
     * @return 打卡详情
     */
    @GetMapping("/{checkinId}")
    public Result<CheckinVO> getCheckinDetail(@PathVariable(value = "checkinId") Long checkinId) {
        try {
            CheckinVO checkinVO = userCheckinsService.getCheckinDetail(checkinId);
            return success(checkinVO);
        } catch (Exception e) {
            return fail(500, e.getMessage());
        }
    }

    /**
     * 更新打卡信息
     *
     * @param checkinId  打卡记录ID
     * @param checkinDTO 打卡信息
     * @return 更新结果
     */
    @PutMapping("/{checkinId}")
    public Result<CheckinVO> updateCheckin(@PathVariable(value = "checkinId") Long checkinId, @RequestBody CheckinDTO checkinDTO) {
        try {
            CheckinVO checkinVO = userCheckinsService.updateCheckin(checkinId, checkinDTO);
            return success(checkinVO);
        } catch (Exception e) {
            return fail(500, e.getMessage());
        }
    }

    /**
     * 删除打卡记录
     *
     * @param checkinId 打卡记录ID
     * @return 删除结果
     */
    @DeleteMapping("/{checkinId}")
    public Result<Boolean> deleteCheckin(@PathVariable(value = "checkinId") Long checkinId) {
        try {
            boolean result = userCheckinsService.deleteCheckin(checkinId);
            return success(result);
        } catch (Exception e) {
            return fail(500, e.getMessage());
        }
    }
}